//
//  ZYCAttendanceCommentCell.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/3.
//

#import "ZYCAttendanceCommentCell.h"
#import "ZYCTeachingEvaluatedDetailModel.h"

@interface ZYCAttendanceCommentCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *scoreRangeLabel;
@property (nonatomic, strong) UILabel *lineOne;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *lineTwo;
@property (nonatomic, strong) UILabel *pleaseLabel;
@property (nonatomic, strong) UITextField *scoreTextField;
@end
@implementation ZYCAttendanceCommentCell

- (void)setModel:(ZYCTeachingEvaluatedSingleModel *)model{
    _model = model;
    _titleLabel.text = model.project;
    _scoreRangeLabel.text = [NSString stringWithFormat:@"0-%zd分",[model.fraction integerValue]];
    _contentLabel.text = model.explainDesc;
    _scoreTextField.text = model.doScore;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.scoreRangeLabel];
        [self.contentView addSubview:self.lineOne];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.lineTwo];
        [self.contentView addSubview:self.pleaseLabel];
        [self.contentView addSubview:self.scoreTextField];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.contentView).offset(15.0f);
        }];
        [self.scoreRangeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-15.0f);
            make.centerY.equalTo(self.titleLabel);
        }];
        
        [self.lineOne mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15.0f);
            make.right.equalTo(self.contentView).offset(-15.0f);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(15.0f);
            make.height.mas_equalTo(1.0f);
        }];
        
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15.0f);
            make.right.equalTo(self.contentView).offset(-15.0f);
            make.top.equalTo(self.lineOne.mas_bottom).offset(16.0f);
        }];
        
        [self.lineTwo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15.0f);
            make.right.equalTo(self.contentView).offset(-15.0f);
            make.top.equalTo(self.contentLabel.mas_bottom).offset(15.0f);
            make.height.mas_equalTo(1.0f);
        }];
        
        [self.pleaseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15.0f);
            make.top.equalTo(self.lineTwo.mas_bottom).offset(15.0f);
            make.bottom.equalTo(self.contentView).offset(-16.0f);
        }];
        
        [self.scoreTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.pleaseLabel.mas_right).offset(15.0f);
            make.centerY.equalTo(self.pleaseLabel);
            make.right.equalTo(self.contentView).offset(-15.0f);
            make.height.mas_equalTo(30.0f);
        }];
        
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldEditChanged:)
//                  name:UITextFieldTextDidChangeNotification object:self.scoreTextField];

    }
    return self;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Light", 16.0f) titleColor:HEX_RGB(@"#2B302D") textAlignment:ZZTextAlignmentLeft];
    }
    return _titleLabel;
}

- (UILabel *)scoreRangeLabel {
    if (!_scoreRangeLabel) {
        _scoreRangeLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Light", 16.0f) titleColor:HEX_RGB(@"#39A85B") textAlignment:ZZTextAlignmentRight];
    }
    return _scoreRangeLabel;
}



- (UILabel *)lineOne {
    if (!_lineOne) {
        _lineOne = [[UILabel alloc] init];
        _lineOne.backgroundColor = HEX_RGB(@"#EBEBEB");
    }
    return _lineOne;
}

- (UILabel *)lineTwo {
    if (!_lineTwo) {
        _lineTwo = [[UILabel alloc] init];
        _lineTwo.backgroundColor = HEX_RGB(@"#EBEBEB");
    }
    return _lineTwo;
}

- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Light", 16.0f) titleColor:HEX_RGB(@"#999999") textAlignment:ZZTextAlignmentLeft];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

- (UILabel *)pleaseLabel {
    if (!_pleaseLabel) {
        _pleaseLabel = [UILabel LabelWithString:@"请输入分值:" Font:kCustomFont(@"PingFangSC-Light", 16.0f) titleColor:HEX_RGB(@"#39A85B") textAlignment:ZZTextAlignmentLeft];
    }
    return _pleaseLabel;
}

- (UITextField *)scoreTextField {
    if (!_scoreTextField) {
        _scoreTextField = [[UITextField alloc] init];
        _scoreTextField.keyboardType = UIKeyboardTypeNumberPad;
        _scoreTextField.placeholder = @"请输入分值";
        _scoreTextField.font = kCustomFont(@"PingFangSC-Light", 16.0f);
        _scoreTextField.textColor = HEX_RGB(kTitleThemeColor);
        _scoreTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _scoreTextField.textAlignment = NSTextAlignmentRight;
        _scoreTextField.delegate = self;
    }
    return _scoreTextField;
}


#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([text integerValue] > [self.model.fraction integerValue] || [text integerValue] < 0) {
        return NO;
    }else{
        return YES;
    }

}


//-(void)textFieldEditChanged:(NSNotification *)noti{
//    UITextField *textField = (UITextField *)noti.object;
//    NSString *toBeString = textField.text;
//    
//    //获取高亮部分
//    UITextRange *selectedRange = [textField markedTextRange];
//    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
//    
//    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
//    if (!position)
//    {
//        if ([toBeString integerValue] > [self.model.fraction integerValue])
//        {
//            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:[self.model.fraction integerValue]];
//            if (rangeIndex.length == 1)
//            {
//                textField.text = [toBeString substringToIndex:[self.model.fraction integerValue]];
//            }
//            else
//            {
//                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, [self.model.fraction integerValue])];
//                textField.text = [toBeString substringWithRange:rangeRange];
//            }
//        }
//    }
//}



- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.saveBlock) {
        self.saveBlock(self.model, textField.text);
    }
}
//
//- (void)textFieldDidChangeValue:(NSNotification *)notification
//{
//    UITextField *sender = (UITextField *)[notification object];
//    NSLog(@"值监听:%@",sender.text);
//
//    if (self.saveBlock) {
//        self.saveBlock(self.model, sender.text);
//    }
//
//
//}
//
//- (void)dealloc{
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}
@end
